package kr.yoon.practice.serviceUI;

import javax.swing.JPanel;

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JScrollPane;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.SwingConstants;

import kr.yoon.practice.db.common.ConnectionFactory;
import kr.yoon.practice.db.dao.DaoInterface;
import kr.yoon.practice.db.dao.DepartmentDAO;
import kr.yoon.practice.db.dto.Department;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.SQLException;
import java.awt.TextField;

public class PanelDepartmentUI extends JPanel implements ActionListener {
	private JTextField tfDeptNo;
	private JTextField tfDeptName;
	private JTextField tfDeptFloor;
	private DepartmentDAO deptDao;
	public JButton btnAction;
	private JPanel panel_bottom;
	/**
	 * Create the panel.
	 */
	public PanelDepartmentUI() {
		setLayout(new BorderLayout(0, 0));
		
		Connection con = ConnectionFactory.getConnection(); //DB연결..
		deptDao = new DepartmentDAO(con);
		
		JPanel panel_center = new JPanel();
		add(panel_center, BorderLayout.CENTER);
		panel_center.setLayout(new GridLayout(3, 2, 0, 0));
		
		JLabel lblDeptNo = new JLabel("부서번호");
		lblDeptNo.setHorizontalAlignment(SwingConstants.CENTER);
		panel_center.add(lblDeptNo);
		
		tfDeptNo = new JTextField();
		panel_center.add(tfDeptNo);
		tfDeptNo.setColumns(10);
		
		JLabel lblDeptName = new JLabel("부서명");
		lblDeptName.setHorizontalAlignment(SwingConstants.CENTER);
		panel_center.add(lblDeptName);
		
		tfDeptName = new JTextField();
		panel_center.add(tfDeptName);
		tfDeptName.setColumns(10);
		
		JLabel lblDeptFloor = new JLabel("위치");
		lblDeptFloor.setHorizontalAlignment(SwingConstants.CENTER);
		panel_center.add(lblDeptFloor);
		
		tfDeptFloor = new JTextField();
		panel_center.add(tfDeptFloor);
		tfDeptFloor.setColumns(10);
		
		panel_bottom = new JPanel();
		add(panel_bottom, BorderLayout.SOUTH);
		
		btnAction = new JButton("action");
		btnAction.addActionListener(this);
		panel_bottom.add(btnAction);
	}
	
	public String getBtnName(){
		return btnAction.getText();
	}

	public void actionPerformed(ActionEvent e) {
		String str = getBtnName();
		switch(str){
			case "추가" :
				actionPerformedBtnActionAdd(e);
				break;
			case "삭제" : 
				actionPerformedBtnActionDel(e);
				break;
			case "수정" : 
				actionPerformedBtnActionUp(e);
				break;
			case "검색" : 
				actionPerformedBtnActionSearch(e);
				break;
			default : 
				break;
				
		}
	}
	
	protected void actionPerformedBtnActionAdd(ActionEvent e) {
		int dptNo = Integer.parseInt(tfDeptNo.getText().trim());
		String dptName = tfDeptName.getText().trim();
		int dptFloor = Integer.parseInt(tfDeptFloor.getText().trim());
//		System.out.println(dptName);
		Department dpt = new Department(dptNo, dptName , dptFloor);
		try {
			deptDao.insertItem(dpt);
//			JOptionPane.showMessageDialog(this, "입력 성공");
			JOptionPane.showMessageDialog(this, dpt);
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "입력 실패");
		}
		panel_bottom.repaint();
		tfDeptNo.setText("");
		tfDeptName.setText("");
		tfDeptFloor.setText("");
	}
	
	protected void actionPerformedBtnActionDel(ActionEvent e) {
		int dptNo = Integer.parseInt(tfDeptNo.getText().trim());
//		String dptName = tfDeptName.getText().trim();
//		int dptFloor = Integer.parseInt(tfDeptFloor.getText().trim());
		Department dpt = new Department(dptNo);
		try {
			deptDao.deleteItemByNo(dpt);
			JOptionPane.showMessageDialog(this, "삭제 성공");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "삭제 실패");
		}
		
		panel_bottom.repaint();
		tfDeptNo.setText("");
		tfDeptName.setText("");
		tfDeptFloor.setText("");
	}
	
	protected void actionPerformedBtnActionUp(ActionEvent e) {
		int dptNo = Integer.parseInt(tfDeptNo.getText().trim());
		String dptName = tfDeptName.getText().trim();
		int dptFloor = Integer.parseInt(tfDeptFloor.getText().trim());
		Department dpt = new Department(dptNo, dptName, dptFloor);
		try {
			deptDao.updateItemByNo(dpt);
			JOptionPane.showMessageDialog(this, "수정 성공");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "수정 실패");
		}
		
		panel_bottom.repaint();
		tfDeptNo.setText("");
		tfDeptName.setText("");
		tfDeptFloor.setText("");
	} //업데이트 수정해야됨...
	
	protected void actionPerformedBtnActionSearch(ActionEvent e) {
		int dptNo = Integer.parseInt(tfDeptNo.getText().trim());
/*		String dptName = tfDeptName.getText().trim();
		int dptFloor = Integer.parseInt(tfDeptFloor.getText().trim());*/
		
//		Department dpt = new Department(dptNo);
		
		Department dpt = new Department();
		dpt.setDeptNo(dptNo);
		
			try {
				dpt = deptDao.searchItem(dpt);
				if(dpt != null){
					JOptionPane.showMessageDialog(null, dpt);
				}else{
					JOptionPane.showMessageDialog(null, "부서정보 없음");
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		panel_bottom.repaint();
		tfDeptNo.setText("");
		tfDeptName.setText("");
		tfDeptFloor.setText("");
	}
}
